<?php
/**
 * Created by JetBrains PhpStorm.
 * User: andrei
 * Date: 29.08.12
 * Time: 16:03
 * To change this template use File | Settings | File Templates.
 */

class Page_Be_Cms_Pages extends Page {

    /**
     * @var Db_Cms_Pages
     */
    protected $tableObject;

    function __construct(){
        self::$TPL->appendJsFile('admin/pages');
		/* the object is loaded from DB for the all the pagination actions because we need to access some of the page's properties*/
		$this->actionsTableObject = array('addEditAction','saveAction', 'configPaginationAction', 'addEditPaginationAction',
			'configBulletsAction', 'savePaginationAction', 'resetProductPageUrlsAction');
		$this->classTableObject = 'Db_Cms_Pages';
		$this->idTableObject = get('id', 'int');
        parent::__construct();

    }

    public function defaultAction(){
		if (perm('PAGES_EDIT')) {
        	$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
		}
		self::$TPL->appendCssFile('admin/pages');
        self::$TPL->setBodyTemplate('list_pages');
        return array(
            'type' => 'display'
        );
    }


    /**
     * @return array
     */
    public function addEditAction() {
		perm('PAGES_EDIT', '/cms/pages');
        if (isset($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . $this->tableObject->objectArray->p_name . '&quot;', '?action=addEdit&id=' . $this->idTableObject, true );
			$allCategories = Db_Products_Categories::getCategoriesByPageId($this->tableObject->objectArray->id);
			self::$TPL->assign('allCategories', $allCategories);

			$allTags = Db_Products_Tags::findAllArray();
			self::$TPL->assign('allTags', $allTags);

			$products = conf('DEFAULT_ITEMS_PER_PAGE');

			if ($this->tableObject->objectArray->p_pagination_type == 'category' || $this->tableObject->objectArray->p_pagination_type == 'poems' || $this->tableObject->objectArray->p_pagination_type == 'tags') {

				if ($this->tableObject->objectArray->p_pagination_type == 'category') {
					$products = Db_Products_Products::getNrOfProductsByPage($this->tableObject->objectArray->id);
				}

				if ($this->tableObject->objectArray->p_pagination_type == 'poems') {
					$projectIdentifiers = Db_Cms_MenuPages::getProjectIdentifiersByPageId($this->tableObject->objectArray->id);
					if($projectIdentifiers) {
						$products = Db_Products_Poems::countPoemsByProjectIdentifier($projectIdentifiers);
					}
				}

				if($this->tableObject->objectArray->p_pagination_type == 'tags') {
					$products = Db_Products_Products::countProductsByTagId($this->tableObject->objectArray->p_pagination_tag_id);
				}

				$itemsPerPage = ($this->tableObject->objectArray->p_items_result_per_page != 0 ? $this->tableObject->objectArray->p_items_result_per_page : conf('DEFAULT_ITEMS_PER_PAGE'));
				$sumPagePaginationNeed = $products / $itemsPerPage;
				$pageNumber = ceil($sumPagePaginationNeed);
				$pagePaginationNumber = Db_Cms_PagePagination::getTotalPagePagination($this->tableObject->objectArray->id);
				if($pageNumber > intval($pagePaginationNumber)) {
					$pageNumber = $pageNumber - $pagePaginationNumber;
					self::$TPL->assign('pageNumber', $pageNumber);
				}
			}



		} else {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true );
        }

		$dynamicUrlCheckBoxDisabled = Db_Cms_PagePagination::checkExists($this->tableObject->id, 'pp_page_id');
		self::$TPL->assign('dynamicUrlCheckBoxDisabled', $dynamicUrlCheckBoxDisabled);

        $allPermissions = Db_Accounts_Permissions::findAllArray();
        self::$TPL->assign('allPermissions', $allPermissions);

        self::$TPL->setBodyTemplate('add_edit_page');

        self::$TPL->assign('editPage', $this->tableObject->objectArray);

        $pageTypes = Db_Cms_PageTypes::getElementsForSelect('pt_title');
        self::$TPL->assign('pageTypes',$pageTypes);

		$htmlTranslationsModule = Db_Locale_Translations::getHtmlTranslations();
		self::$TPL->assign('htmlTranslationsModule', $htmlTranslationsModule);

        $cacheGroups = Db_CacheGroups::getElementsForSelect('cg_key');
        self::$TPL->assign('cacheGroups',$cacheGroups);

		$menus = Db_Cms_Menus::findAll();
		self::$TPL->assign('menus', $menus);
		if(!empty($this->tableObject->objectArray->id)) {
			$usedMenus = Db_Cms_MenuPages::getUsedMenusByPageId($this->tableObject->objectArray->id);
		}
		self::$TPL->assign('usedMenus', $usedMenus);

        return array(
            'type'=>'display'
        );
    }

    public function saveAction() {
		perm('PAGES_EDIT', '/cms/pages');
		$paginationType = post('p_pagination_type');
		$menus = post('mp_menu_id');
		$inProjects = Db_Projects::getProjectsIdentifierByMenus($menus);
		$types = array();
		if($this->tableObject->objectArray->id) {
			$countPagePagination = Db_Cms_PagePagination::countAllByField('pp_page_id', $this->tableObject->objectArray->id);
			$notHavePagination = ($countPagePagination > 0 && $this->tableObject->objectArray->p_pagination_type != $paginationType ? true : false );
		} else {
			$notHavePagination = false;
		}

		if ($paginationType == 'product') {
			if ($pageUsed = $this->tableObject->checkPageIsUniqueInProjectByType('product', $this->tableObject->objectArray->id, $inProjects)) {
				$types[] = 'product';
			}
		}
		if (post('p_is_login', 'int')) {
			if ($this->tableObject->checkPageIsUniqueInProjectByType('login', $this->tableObject->objectArray->id, $inProjects)) {
				$types[] = 'login';
			}
		}
		if (post('p_is_index', 'int')) {
			if ($this->tableObject->checkPageIsUniqueInProjectByType('index', $this->tableObject->objectArray->id, $inProjects)) {
				$types[] = 'index';
			}
		}
		if (post('p_404_error', 'int')) {
			if ($this->tableObject->checkPageIsUniqueInProjectByType('404', $this->tableObject->objectArray->id, $inProjects)) {
				$types[] = '404';
			}
		}

		if (count($types) > 0) {
			$this->error = true;
			$projects = array();
			$projectsName = null;
			if(isset($pageUsed) && is_array($pageUsed)) {
				foreach ($pageUsed as $project) {
					$projects[] = ct(T_PROJECT_TITLE, $project['pr_identifier']);
				}
				$projectsName = t(T_FORM_ERROR, 'in') . implode(', ', $projects);
			}
			$this->message = implode(',', $types) . ' ' . t(T_FORM_ERROR, 'page_types_already_exists') . $projectsName;
			return array(
				'type' => 'redirect',
				'content' => '?action=addEdit&id=' . $this->tableObject->id
			);
		}

		$permissionId = post('p_permission_id', 'int');
		$isLogin = post('p_is_login', 'int');
		$isIndex =  post('p_is_index', 'int');
		$noIndexFollow =  post('p_no_index_follow', 'int');
		$pError = post('p_404_error', 'int');
        $url = post('p_url', 'str');
		$dynamicUrl = post('p_dynamic_url', 'int');
		$controller = post('p_controller', 'str');
		$title = strtolower(post('p_title', 'str'));
		$metaTags = post('p_meta_tags', 'str');
		$metaDescription = post('p_meta_description', 'str');
		$beforeHead = post('p_before_head', 'str', true);
		$beforeBody = post('p_before_body', 'str', true);
		$googleAnalytics = post('p_google_analytics', 'str', true);
		$googleAnalyticsGlobal = post('p_google_analytics_global', 'int');
		$showOnSiteMap =  post('p_show_on_sitemap', 'int');
        $cacheGroupId = post('p_cache_group_id', 'int');

		if (post('hidden_menu_group', 'int') == 1) {
			$permissionId = 0;
			$isLogin = 0;
			$isIndex =  0;
			$noIndexFollow =  0;
			$pError = 0;
			$url = "";
			$dynamicUrl = 0;
			$controller = "";
			$title = "";
			$metaTags = "";
			$metaDescription = "";
			$beforeHead = "";
			$beforeBody = "";
			$googleAnalytics = "";
			$googleAnalyticsGlobal = 0;
			$showOnSiteMap = 1;
			$cacheGroupId = 0;
		}

		$this->tableObject->p_name = strtolower(post('p_name', 'str'));
        $this->tableObject->p_type_id = post('p_type_id', 'int');
        $this->tableObject->p_permission_id = $permissionId;
        $this->tableObject->p_url = $url;
        $this->tableObject->p_is_index = $isIndex;
        $this->tableObject->p_is_login = $isLogin;
        $this->tableObject->p_dynamic_url = $dynamicUrl;
        $this->tableObject->p_controller = $controller;
        $this->tableObject->p_title = $title;
        $this->tableObject->p_dynamic_title = post('p_dynamic_title', 'int');
        $this->tableObject->p_meta_tags = $metaTags;
        $this->tableObject->p_meta_description = $metaDescription;
        $this->tableObject->p_before_head = $beforeHead;
        $this->tableObject->p_before_body = $beforeBody;
        $this->tableObject->p_google_analytics = $googleAnalytics;
        $this->tableObject->p_google_analytics_global = $googleAnalyticsGlobal;
        $this->tableObject->p_show_on_sitemap = $showOnSiteMap;
        $this->tableObject->p_no_index_follow = $noIndexFollow;
        $this->tableObject->p_404_error = $pError;
        $this->tableObject->p_cache_group_id = $cacheGroupId;
        $this->tableObject->p_translation_module = (post('p_translation_key')!='') ? (post('p_translation_module')!='' ? post('p_translation_module') : '' ) : '';
        $this->tableObject->p_translation_key = (post('p_translation_key')!='' ? post('p_translation_key') : '' );
		$this->tableObject->p_pagination_type = ($paginationType!='' ? $paginationType : '' );
		$this->tableObject->p_items_result_per_page = post('items_per_page', 'int');
		$this->tableObject->p_pagination_category_id = ($paginationType == 'category' ? (post('p_pagination_category_id')!='' ? post('p_pagination_category_id') : '' ) : '' );
		$this->tableObject->p_pagination_tag_id = ($this->tableObject->p_pagination_type == 'tags' ? post('p_pagination_tag_id', 'int') : null);

		$pageUrl = post('p_url');

		if (!$this->tableObject->checkPageUrlUnique($pageUrl, $this->tableObject->id, 'page', $inProjects) && !$notHavePagination) {

			if($this->tableObject->save()) {
				$this->message = t(T_FORM_MSG, 'success');
			} else {
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
				$this->error = true;
			}

			Db_Cms_MenuPages::deleteByFieldsNotInArray(array('mp_menu_id' => $menus,
														 'mp_page_id' => $this->tableObject->id
			));

			foreach($menus as $menuId) {
				$menuPages = new Db_Cms_MenuPages();
				$thisMenuId = Db_Cms_MenuPages::checkForDuplicate(false, $menuId, $this->tableObject->id);
				if (empty($thisMenuId)) {
					$menuPages->positionCommonFields = array(
						'mp_menu_id' => $menuId
					);
					$menuPages->mp_menu_id = $menuId;
					$menuPages->mp_page_id = $this->tableObject->id;
					$menuPages->mp_item_title = strtolower(post('p_name', 'str'));
					$menuPages->mp_display = true;
					$menuPages->save();
					$menuPages->id = (isset($menuPages->id) ? $menuPages->id : null);
					$menuPages->positionSetLast();
				} else {
					$menuPage =  Db_Cms_MenuPages::findById($thisMenuId);
					if (empty($menuPage->mp_item_title)) {
						$menuPages->mp_menu_id = $menuId;
						$menuPages->mp_page_id = $this->tableObject->id;
						$menuPages->mp_item_title = strtolower(post('p_name', 'str'));
						$menuPages->save(true);
				    }
				}
			}
		} else {
			$this->message = t(T_FORM_ERROR, 'this_page_url_exist') . ', ' . t(T_FORM_ERROR, 'page_contains_pagination_items');

			$this->error = true;
		}
		if ($paginationType == 'product') {
			Db_Cms_Pages::updateProductPageUrls();
		}

        return array(
            'type' => 'redirect',
            'content' => '?action=addEdit&id=' . $this->tableObject->id
        );

    }

	public function configPaginationAction(){
		perm('PAGES_EDIT', '/cms/pages');
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . $this->tableObject->objectArray->p_name . '&quot;', '?action=addEdit&id=' . $this->idTableObject );
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'config_pagination'), '?action=configPagination&id=' . $this->idTableObject, true);
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEditPagination&id=' . $this->idTableObject, true);
		self::$TPL->appendJsFile('admin/paginations');
		$allPagePaginations = Db_Cms_PagePagination::findAllPaginationsForPage(get('id', 'int'));
		self::$TPL->assign('allPagePaginations', $allPagePaginations);
		self::$TPL->assign('editPage', $this->tableObject->objectArray);
		self::$TPL->setBodyTemplate('list_paginations');
		return array(
			'type' => 'display'
		);
	}

	public function deletePaginationAction(){
		perm('PAGES_EDIT', '/cms/pages');
		$pageId = get('id', 'int');
		$paginationId = get('paginationId', 'int');
		$this->message = t(T_FORM_MSG, 'success');
		if (!empty($paginationId)){
			Db_Cms_PageCategoryBullets::abstractDeleteByField('pcb_page_pagination_id', $paginationId);
			if (!Db_Cms_PagePagination::abstractDelete($paginationId)) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		} else {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'content' => '?action=configPagination&id=' . $pageId
		);
	}

	public function addEditPaginationAction(){
		perm('PAGES_EDIT', '/cms/pages');
		if(!empty($this->tableObject->objectArray->id) && $this->tableObject->objectArray->p_pagination_type == 'product') {
			return array(
				'type' => 'redirect',
				'query' => false
			);
		}
		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . $this->tableObject->objectArray->p_name . '&quot;', '?action=addEdit&id=' . $this->idTableObject );
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'config_pagination'), '?action=configPagination&id=' . $this->idTableObject);
			$allCategories = Db_Products_Categories::getCategoriesByPageId($this->tableObject->objectArray->id);
			self::$TPL->assign('allCategories', $allCategories);
			self::$TPL->assign('editPage', $this->tableObject->objectArray);

			if (get('paginationId', 'int')) {
				$pagination = Db_Cms_PagePagination::findById(get('paginationId', 'int'));
				$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit'), '?action=addEditPagination&id=' . $this->idTableObject . '&paginationId=' . get('paginationId', 'int'), true);
			}else{
				$pagination = null;
				$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEditPagination&id=' . $this->idTableObject, true);
			}
			$categoryId = (!empty($pagination->pp_category_id) ? $pagination->pp_category_id : $allCategories[0]['id']);
			$photosProductCategory = Db_Products_Photos::getPhotosOfProductsByCategoryId($categoryId);
			self::$DEBUG->addPrivateVar($photosProductCategory);
			self::$TPL->assign('photosProductCategory', $photosProductCategory);
			self::$TPL->assign('editPagination', $pagination);
		} else {
			return array(
				'type' => 'redirect',
				'query' => false
			);
		}

		self::$TPL->appendJsFile('admin/paginations');
		self::$TPL->setBodyTemplate('add_edit_pagination');
		return array(
			'type' => 'display'
		);
	}


	public function savePaginationAction(){
		perm('PAGES_EDIT', '/cms/pages');
		$pageId = get('id', 'int');
		$photoId = post('ph_photos');
		if ($this->tableObject->objectArray->id) {
			//get menus of parent page
			$menusArray = Db_Cms_MenuPages::getUsedMenusByPageId($this->tableObject->objectArray->id);
			$inProjects = Db_Projects::getProjectsIdentifierByMenus($menusArray);
			if ($this->tableObject->checkPageUrlUnique(post('pp_page_url', 'string'),get('paginationId', 'int'), 'pagination', $inProjects)) {
				$this->message = t(T_FORM_MSG, 'unknown_error_occurred');
				$this->error = true;
				return array(
					'type' => 'redirect',
					'content' => '?action=configPagination&id=' . $pageId
				);
			}
			$newPagination = new Db_Cms_PagePagination(get('paginationId', 'int'));
			$newPagination->pp_page_id = $pageId;
			$newPagination->pp_pagination_number = post('pp_pagination_number', 'int');
			$newPagination->pp_page_title = strtolower(post('pp_page_title', 'string'));
			$newPagination->pp_page_url = post('pp_page_url', 'string');
			$newPagination->pp_head_line = post('pp_head_line', 'string');
			$newPagination->pp_sub_head_line = post('pp_sub_head_line', 'string');
			$newPagination->pp_category_id = post('pp_category_id', 'string');
			$newPagination->pp_photo_alt_tag = post('pp_photo_alt_tag', 'string');
			$newPagination->pp_photo_id = (!empty($photoId) ? $photoId : '');
			$newPagination->pp_photo_name = post('pp_photo_name', 'string');
			if ($newPagination->save(true)) {
				$this->message = t(T_FORM_MSG, 'success');
			} else {
				$this->message = t(T_FORM_MSG, 'unknown_error_occurred');
				$this->error = true;
			}
		} else {
			$this->message = t(T_FORM_MSG, 'unknown_error_occurred');
			$this->error = true;
		}

		return array(
			'type' => 'redirect',
			'content' => '?action=configPagination&id=' . $pageId
		);
	}

	public function configBulletsAction(){
		perm('PAGES_EDIT', '/cms/pages');
		$paginationId = get('paginationId', 'int');
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' &quot;' . $this->tableObject->objectArray->p_name . '&quot;',
			'?action=addEdit&id=' . $this->idTableObject );
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'config_pagination'), '?action=configPagination&id=' . $this->idTableObject);
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'config_bullets'), '?action=configBullets&id=' . $this->idTableObject .
			'&paginationId=' . $paginationId, true);
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '#add_bullet' );
		$allBullets = Db_Cms_PageCategoryBullets::findAllByField('pcb_page_pagination_id', $paginationId);
		self::$TPL->assign('allBullets', $allBullets);
		self::$TPL->appendJsFile('admin/paginations');
		self::$TPL->setBodyTemplate('list_bullets');
		return array(
			'type' => 'display'
		);
	}

	public function saveBulletsAction(){
		perm('PAGES_EDIT', '/cms/pages');
		$bullets = post('bullets', 'array');
		$pageId = get('id', 'get');
		$paginationId = get('paginationId', 'get');
		$bulletDbObject = new Db_Cms_PageCategoryBullets();
		foreach($bullets as $bullet){
			$bulletDbObject->id = null;
			$bulletDbObject->pcb_page_pagination_id = $paginationId;
			$bulletDbObject->pcb_number = $bullet['nr'];
			$bulletDbObject->pcb_content = strtolower($bullet['content']);
			$bulletDbObject->save(true);
		}
		return array(
			'type' => 'redirect',
			'content' => "?action=configBullets&paginationId=$paginationId&id=$pageId"
		);
	}

	public function deleteBulletAction(){
		perm('PAGES_EDIT', '/cms/pages');
		$pageId = get('id', 'get');
		$paginationId = get('paginationId', 'get');
		$bulletId = get('bulletId', 'get');
		$this->message  = t(T_FORM_MSG, 'success');
		if (!empty($bulletId)) {
			if(!Db_Cms_PageCategoryBullets::abstractDelete($bulletId)) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		} else {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'content' => "?action=configBullets&paginationId=$paginationId&id=$pageId"
		);
	}

	public function editBulletAction(){
		perm('PAGES_EDIT', '/cms/pages');
		$bulletId = post('bulletId', 'int');
		$fieldName = post('fieldName', 'string');
		$value = post('value');
		$bullet = Db_Cms_PageCategoryBullets::findById($bulletId);
		$oldValue = $bullet->{$fieldName};
		if($oldValue != $value){
			$bullet->{$fieldName} = $value;
			if(!$bullet->save() ){
				$value = $oldValue;
			}
		}
		return array(
			'type' => 'string',
			'content' => $value
		);
	}

    /**
     * @return array
     */
    public function loadPagesDataAction() {
		perm('PAGES_EDIT', '/cms/pages');
        $allColumns = array('p_name', 'pt_title', 'p_url', 'p_dynamic_url','p_is_index','p_controller','p_title', 'action');
        $searchColumns = array('p_name', 'pt_title', 'p_url', 'p_dynamic_url','p_is_index','p_controller','p_title');

        // Ordering
		list($searchQuery,$orderBy) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns);

		if($searchQuery !== false) {
			// Paging
			$totalElements = Db_Cms_Pages::getTotalElements();
			$totalFilteredElements = Db_Cms_Pages::getTotalElements($searchQuery);
			$limit = '';
			if (post('iDisplayLength', 'int') > 0) {
				$limit = "LIMIT ".post('iDisplayStart', 'int').', '.post('iDisplayLength', 'int');
			}

			$allProductsPage = 12; //for this var need get real products by page categories (category, poems)
			self::$TPL->assign('allProductsPage', $allProductsPage);

			$pagesList = Db_Cms_Pages::loadAllPages($limit, $searchQuery, $orderBy);
			self::$TPL->assign('totalElements', $totalElements);
			self::$TPL->assign('totalFilteredElements', $totalFilteredElements);
			self::$TPL->assign('pagesList',$pagesList);

			$menus = Db_Cms_Menus::getMenusPages();
			self::$TPL->assign('menus', $menus);
		}

        return array(
            'type' => 'encoded_json',
            'content' => self::$TPL->display('json_list_pages', true)
        );
    }

    public function deleteAction() {
		perm('PAGES_EDIT', '/cms/pages');
        $pagesId = get('id', 'int');
        $this->message  = t(T_FORM_MSG, 'success');
        if (!empty($pagesId)) {
            if(Db_Cms_Pages::abstractDelete($pagesId)) {
				Db_Cms_Pages::deleteAllChildrenPage($pagesId);
			} else {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        return array(
            'type' => 'redirect',
            'query' => false
        );
    }

	public function resetProductPageUrlsAction() {
		perm('PAGES_EDIT', '/cms/pages');
		if (!$this->idTableObject) {
			return array(
				'type' => 'redirect',
				'query' => false
			);
		}
		Db_Cms_PagePagination::abstractDeleteByField('pp_page_id', $this->tableObject->objectArray->id);
		//exit;
		list($insert) = $this->tableObject->updateProductPageUrls($this->tableObject->objectArray->id);

		$this->message = t(T_FORM_MSG, 'update') . ' - ' .count($insert);
		return array(
			'type' => 'redirect',
			'content' => '?action=configPagination&id=' . $this->tableObject->objectArray->id
		);

	}

	public function ajaxGetKeysByModuleAction() {
		$module = post('module', 'str');
		$selKey = post('key', 'str');
		$translationKeys = Db_Locale_Translations::getTranslationsKeysByModule($module, true);
		self::$TPL->assign('translationKeys',$translationKeys);
		self::$TPL->assign('selKey', $selKey);
		return array(
			'type' => 'json',
			'content' => self::$TPL->display('translation_keys_options', true)
		);

	}

	public function ajaxGetMetaTagsTranslationsAction() {
		$translationKeys = array();
		if (post('type', 'str') == 'tags') {
			$translationKeys = Db_Locale_Translations::getTranslationsKeysByModule(T_PAGE_META_TAGS);
		} elseif (post('type', 'str') == 'description') {
			$translationKeys = Db_Locale_Translations::getTranslationsKeysByModule(T_PAGE_META_DESCRIPTION);
		}

		return array(
			'type' => 'json',
			'content' => $translationKeys
		);
	}

	public function ajaxCheckPageIsUsedAction(){
		$menus = post('mp_menu_id');
		$pageUrl = post('p_url', 'str');
		$pageId = post('p_id', 'int');

		$inProjects = Db_Projects::getProjectsIdentifierByMenus($menus);

		if (!Db_Cms_Pages::checkPageUrlUnique($pageUrl, $pageId, 'page', $inProjects)) {
			$this->error = false;
		} else {
			$this->error = true;
		}

			return array(
			'type' => 'encoded_json',
			'content' => $this->jsonResult($this->error, true, !$this->error)
		);
	}

	public function ajaxEditPaginationUrlAction() {
		perm('PAGES_EDIT', '/cms/pages');
		$paginationId = post('paginationId', 'int');
		$value = post('value', 'str');
		$pagePaginationObj = new Db_Cms_PagePagination($paginationId);
		if ($pagePaginationObj->objectArray->id) {
			$oldValue = $pagePaginationObj->objectArray->pp_page_url;
			if ($value != $oldValue) {
				$pagePaginationObj->pp_page_url = $value;
				if (!$pagePaginationObj->save()) {
					$value = $oldValue;
				}
			}
			return array(
				'type' => 'string',
				'content' => $value
			);
		}
		return array(
			'type' => 'string',
			'content' => $value
		);
	}

	public function ajaxGetPhotosByProductCategoryAction() {
		$categoryId = post('category_id', 'int');
		$photosProductCategory = Db_Products_Photos::getPhotosOfProductsByCategoryId($categoryId);
		$photosProductCategory = (!empty($photosProductCategory) ? $photosProductCategory : false );
		return array(
			'type' => 'encoded_json',
			'content' => $this->jsonResult($this->error, true, $photosProductCategory)
		);
	}


}